package com.john.utils.page;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.john.utils.enums.PageEnums;
import com.john.utils.enums.ResultStatus;
import com.john.utils.result.AjaxResult;

import java.util.List;

/**
 * 自定义分页插件
 */
public class PageInfoUtils<T> {
    // 当前页
    private Integer pageNum = 1;
    // 单页显示数据条数
    private Integer pageSize = PageEnums.P20.getCode();
    //起始行
    @JsonIgnore  //返回时排除掉这个字段
    private Integer startRow;
    //末行
    @JsonIgnore  //返回时排除掉这个字段
    private Integer endRow;
    // 总的数据条数
    @JsonIgnore  //返回时排除掉这个字段
    private Integer totalRecord;
    // 总页数
    private Integer totalPage;
    // 数据集合
    private List<T> rows;

    @JsonIgnore  //返回时排除掉这个字段
    private AjaxResult ajaxResult;

    public PageInfoUtils(){

    }
    public PageInfoUtils(PageEnums pageEnums) {
        this.pageSize = pageEnums.getCode();
    }
    public PageInfoUtils(Integer pageSize) {
        this.pageSize = pageSize;
    }

    /**
     *
     * @param pageNum
     *            当前页
     * @param pageSize
     *            单页显示数据条数
     * @param totalRecord
     *            总的数据条数
     */
    public PageInfoUtils(Integer pageNum, Integer pageSize, Integer totalRecord, List<T> rows)
    {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        //分页合理化，针对不合理的页码自动处理
        this.pageNum =  (pageNum == null || pageNum <= 0) ? 1 : pageNum;
        this.pageSize = (pageSize == null || pageSize <= 0) ? 20 : pageSize;
        totalRecord = (totalRecord == null || totalRecord <= 0) ? -1 : totalRecord;
        if (totalRecord == -1) {
            totalPage = 1;
            return;
        }
//        this.totalRecord = totalRecord;
        if (pageSize > 0) {
            totalPage = (int) (totalRecord / pageSize + ((totalRecord % pageSize == 0) ? 0 : 1));
        } else {
            totalPage = 0;
        }
        //分页合理化，针对不合理的页码自动处理
        if (this.pageNum > totalPage) {
            this.pageNum = totalPage;
        }
        this.rows = rows;
        this.totalRecord = totalRecord;
        calculateStartAndEndRow();
    }

    /**
     *
     * @param pageNum
     *            当前页
     *            单页显示数据条数
     * @param totalRecord
     *            总的数据条数
     */
    public PageInfoUtils setPageInfo(Integer pageNum, Integer totalRecord)
    {
        this.pageNum = pageNum;
        //分页合理化，针对不合理的页码自动处理
        this.pageNum =  (pageNum == null || pageNum <= 0) ? 1 : pageNum;
        this.pageSize = (pageSize == null || pageSize <= 0) ? 20 : pageSize;
        totalRecord = (totalRecord == null || totalRecord <= 0) ? -1 : totalRecord;
        if (totalRecord == -1) {
            totalPage = 1;
        }else{
            if (pageSize > 0) {
                totalPage = (int) (totalRecord / pageSize + ((totalRecord % pageSize == 0) ? 0 : 1));
            } else {
                totalPage = 0;
            }
            //分页合理化，针对不合理的页码自动处理
            if (this.pageNum > totalPage) {
                this.pageNum = totalPage;
            }
            this.totalRecord = totalRecord;
            calculateStartAndEndRow();
        }
        return this;
    }

    /**
     * 计算总页数
     * @param totalRecord
     * @return
     */
    public Integer getTotalPage(Integer totalRecord){
        if (pageSize > 0) {
            totalPage = (int) (totalRecord / pageSize + ((totalRecord % pageSize == 0) ? 0 : 1));
        } else {
            totalPage = 0;
        }
        return  totalPage;
    }

    /**
     * 计算起止行号
     */
    private void calculateStartAndEndRow() {
        this.startRow = this.pageNum > 0 ? (this.pageNum - 1) * this.pageSize : 0;
        this.endRow = this.startRow + this.pageSize * (this.pageNum > 0 ? 1 : 0);
    }

    /**
     * 参数效验
     * @return true效验通过 false 效验不通过
     */
    public boolean validator(){
        /**如果没有结果集且当前查询页pageNum大于0时**/
        if(this.totalPage == 0 && this.pageNum >= 1){
            this.ajaxResult = new AjaxResult(ResultStatus.SUCCESS.getCode(),ResultStatus.SUCCESS.getMsg(),this);
            return  false;
        }
        /**如果有结果集且pageNum小于0或者大于总页数时**/
        if(this.pageNum <= 0 || this.pageNum > this.totalPage){
            this.ajaxResult = new AjaxResult(ResultStatus.PAGENUM.getCode(),ResultStatus.PAGENUM.getMsg());
            return false;
        }
        /***如果有结果集且pageSize小于20或者大于最大数据条数时*/
        if(this.pageSize < PageEnums.PMINPAGESIZE.getCode() || this.pageSize > PageEnums.PMAXPAGESIZE.getCode()){
            this.ajaxResult = new AjaxResult(ResultStatus.PAGESIZE.getCode(),ResultStatus.PAGESIZE.getMsg());
            return false;
        }
        return true;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public List<T> getRows() {
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }

    public Integer getStartRow() {
        return startRow;
    }

    public void setStartRow(Integer startRow) {
        this.startRow = startRow;
    }

    public Integer getEndRow() {
        return endRow;
    }

    public void setEndRow(Integer endRow) {
        this.endRow = endRow;
    }

    public Integer getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(Integer totalRecord) {
        this.totalRecord = totalRecord;
        if (pageSize > 0) {
            totalPage = (int) (totalRecord / pageSize + ((totalRecord % pageSize == 0) ? 0 : 1));
        } else {
            totalPage = 0;
        }
        calculateStartAndEndRow();
    }

    public AjaxResult getAjaxResult() {
        return ajaxResult;
    }

    public void setAjaxResult(AjaxResult ajaxResult) {
        this.ajaxResult = ajaxResult;
    }

    public static void main(String[] args) {
        Integer totalRecord = 0;
        Integer pageSize = 20;
        Integer totalPage = 0;
        if (pageSize > 0) {
            totalPage = (int) (totalRecord / pageSize + ((totalRecord % pageSize == 0) ? 0 : 1));
        } else {
            totalPage = 0;
        }
        System.out.println(totalPage);
    }
}
